var meauId;
layui.config({
	version:'1510164124181'
}).use(['form', 'laypage', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
	laypage = layui.laypage;//分页
	element = layui.element; //元素操作
	meauId=GetQueryString("meauId");

	$("input[name='parent_info']").click(function(){
		if(this.checked){
			if($(this).val()==1||$(this).val()=='1'){
				$("#parent_menu").show();
			}else{
				$("#parent_menu").hide();
			}
		}
	})

	SelectAllParentList();
	if(!isNotNull(meauId)){
		meauId=0;
	}else{
		initdatas();
	}


});
function initdatas() {
	//加载
	// var index = top.layer.msg('查询中，请稍候',{icon: 16,time:false,shade:0.8});
	$.ajax({
		url: httpurl+"/SelectMenuById",
		cache: false,
		headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
		data: {
			"id":meauId
		},
		success: function(data) {
			// layer.close(index);
			if(data.rtnCode==200){
				let dataInfo=data.data;
				$("#user_name").val(dataInfo.menu_name);//菜单名称
				let parent_info=(dataInfo.parentId==0?0:1);
				$('input[name="parent_info"][value="'+parent_info+'"]').attr("checked",true);//是否为父级菜单
				if (parent_info==1){//有父级
					$("#parent_menu option[value="+dataInfo.parentId+"]").attr("selected",true);
					$("#parent_menu").show();
				}else {
					$("#parent_menu").hide();
				}
				$("#account_target").val(dataInfo.url);//跳转链接
				$("#sort").val(dataInfo.sort);
			}
		}
	});
}


function SelectAllParentList() {
  var open="";
	$.ajax({
		url:httpurl+"/callParent",
		cache: false,
		headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
		async:false,
		success: function (data) {
			// console.log(JSON.stringify((data)));
			var list=data.data;
			for (var i=0;i<list.length;i++){
                  open+=" <option value='"+list[i].id+"'>"+list[i].menu_name+"</option>";
			}
			$("#is_perent_menu").html(open);
		}
	})
}
function save_menu_info(){
	var user_name=$("#user_name").val();//菜单名称
	var parent_info = $('input[name="parent_info"]:checked').val();//是否为父级菜单
	var is_perent_menu=0;
	if (parent_info==1){//否
		is_perent_menu = $("#is_perent_menu option:selected").val();//父级菜单id
	}else {
		is_perent_menu=0;
	}
	var account_target=$("#account_target").val();//跳转链接
	let sort=$("#sort").val();
	if(user_name==""){
		layer.alert("请输入菜单名称!!");
	}
	let ajaxHttp="/updateMenu";
	if(meauId==0){
		ajaxHttp="/insertMenu";
	}
	$.ajax({
		url:httpurl+ajaxHttp,
		type:"POST",
		cache: false,
		headers: {
			"Access-Control-Allow-Origin": "*",
			"Access-Control-Allow-Methods": "*",
			"Access-Control-Allow-Headers": "Authorization",
			"Authorization": GetTokenInfo()
		},
		contentType : 'application/json',
		data:JSON.stringify({
			"id":meauId,
			"sort":sort,
			"menu_name":user_name,
			"parentId":is_perent_menu,
			"level":parent_info,
			"url":account_target
		}),
		success: function (data) {
			console.log(JSON.stringify((data)));
			if(data.rtnCode==200){
				layer.msg(data.msg);
				re();
			}
		},error:function (data) {
			layer.msg("读取接口失败,请稍后再试试");
		}
	});
}
function re() {
	window.location.href="MeauList.html";
}